using System.Collections;
using System.Collections.Generic;
using BCW.Friends;
using BCW.GameTypes.SkyBlock;
using UnityEngine;
using UnityEngine.SceneManagement;

public class controllerConnectGame : PhotonMonoMessagesHandler
{
	public static controllerConnectGame thisScript;

	public static bool reconnectRoom;

	[HideInInspector]
	public Vector3 nearPoint = Vector3.zero;

	private GameObject myPlayer;

	private PlayerBehavior playerScript;

	private int countConnectToRoom;

	public bool ReconnectCanceled;

	private int reconPoint;

	private int reconHealth;

	private int reconMaxHealth;

	private int reconMaxArmor;

	private Vector3 reconPosition;

	private Quaternion reconRotation;

	private double reconTime;

	private PunTeams.Team reconTeam;

	private PunTeams.Team reconMyTeam;

	public LayerMask reconnectReyLayers;

	private int _disconnectRound;

	private EntityBehavior playerInCar;

	private Coroutine _connection;

	private Coroutine _connectionTimeOut;

	public static bool afterRecconect;

	public int reconnectRoundIndex;

	private IEnumerator reconnect;

	private IEnumerator joinRoom;

	private IEnumerator _sendPVPinfo;

	private void Awake()
	{
		thisScript = this;
	}

	private void Start()
	{
		if (settings.offlineMode)
		{
			base.enabled = false;
		}
		else
		{
			PhotonNetwork.autoJoinLobby = true;
		}
	}

	private void OnDestroy()
	{
		reconnectRoom = false;
		thisScript = null;
	}

	private IEnumerator ConnectionTimeOut()
	{
		yield return new WaitForSeconds(7f);
		if (!PhotonNetwork.connected)
		{
			if (_connection != null)
			{
				StopCoroutine(_connection);
				_connection = null;
			}
			if (!settings.offlineMode)
			{
				BCWDebug.LogError("Connection time out!");
				MessageWindow.instance.ShowMessageNoConnection();
				MessageWindow.instance.ToggleBlockPanel(false);
			}
			PhotonNetwork.Disconnect();
			settings.updateOfflineMode(true);
		}
		_connectionTimeOut = null;
	}

	private IEnumerator RoomTimeOut()
	{
		yield return new WaitForSeconds(7f);
		if (PhotonNetwork.room == null || PhotonNetwork.room.name == Load.LoadString(settings.keyRoomName))
		{
			BCWDebug.LogError("Room time out!");
			if (_connection != null)
			{
				StopCoroutine(_connection);
				_connection = null;
			}
			MessageWindow.instance.ShowMessageNoConnection();
			MessageWindow.instance.ToggleBlockPanel(false);
		}
		BCWDebug.LogError("Room exist");
	}

	public void SetRoomTimeOut()
	{
		StartCoroutine(RoomTimeOut());
	}

	public void SetOnline(bool value)
	{
		SaveSettingsPlayer();
		if (value)
		{
			if (_connection == null)
			{
				_connection = StartCoroutine(Connection());
			}
			if (_connectionTimeOut == null)
			{
				_connectionTimeOut = StartCoroutine(ConnectionTimeOut());
			}
		}
		else
		{
			PhotonNetwork.Disconnect();
			settings.updateOfflineMode(true);
			PvPLeaderbord.instance.playersScroll_1.RemovePlayers();
			PvPLeaderbord.instance.playersScroll_2.RemovePlayers();
			InitLevel();
		}
	}

	public IEnumerator Connection()
	{
		if (!PhotonNetwork.connected)
		{
			controllerConnectPhoton.thisScript._initializeWorldwide();
			while (!PhotonNetwork.connected)
			{
				yield return null;
			}
		}
		if (!PhotonNetwork.inRoom)
		{
			controllerConnectPhoton.thisScript.connectRandomRoom(SceneManager.GetActiveScene().name, GameController.instance.curTypeGame);
			while (!PhotonNetwork.inRoom)
			{
				yield return null;
			}
		}
		settings.updateOfflineMode(false);
		_connection = null;
	}

	private void InitLevel()
	{
		GameController.instance.playerScript.DeactivateJetpack();
		if (ManagerMissions.instance != null)
		{
			ManagerMissions.instance.StopAllMission();
		}
		PVPManager.instance.Init();
		MobSpawnController.instance.Init();
		GameController.instance.SetGameMode();
		GameTopWindow.instance.StartInit();
		MessageWindow.instance.ToggleBlockPanel(false);
		GameController.instance.playerScript.canMove = true;
		GameController.instance.playerScript.canShoot = true;
		if (!settings.offlineMode)
		{
			if (PhotonNetwork.isMasterClient)
			{
				ActivateRoomAfterDelay();
			}
			PhotonNetwork.isMessageQueueRunning = true;
		}
	}

	public void instantiateNetworkObjects(TypeGame typeGame = TypeGame.normal)
	{
		BCWDebug.Log("instantiateNetworkObjects " + typeGame);
		GameController.instance.CurrentGameMode = RoundBasedGameModeProvider.GetGameModeInstance(typeGame);
		PhotonNetwork.isMessageQueueRunning = true;
		SpawnPointChief.SpawnPoint spawnPoint = default(SpawnPointChief.SpawnPoint);
		spawnPoint.position = Vector3.up * 1000f;
		spawnPoint.rotation = Quaternion.identity;
		switch (typeGame)
		{
		case TypeGame.race:
			if (PhotonNetwork.room != null && PhotonNetwork.room.customProperties[controllerConnectPhoton._FinalCountdown] != null && !reconnectRoom)
			{
				controllerConnectPhoton.thisScript.LeaveRoom(false);
				return;
			}
			break;
		case TypeGame.tanks:
			if (controllerConnectPhoton.thisScript.changeMap)
			{
			}
			break;
		case TypeGame.Zombe:
			GameInfoWindow.instance.ShowStartGameWindow();
			if (PhotonNetwork.isMasterClient && GameController.instance.curTypeGame == TypeGame.Zombe && bonuseManager.thisScript.listAllBonuse.Count == 0)
			{
				bonuseManager.thisScript.SpawnBonuses();
			}
			break;
		default:
			if (reconnectRoom)
			{
				spawnPoint.position = reconPosition;
				spawnPoint.rotation = reconRotation;
			}
			break;
		case TypeGame.TDM:
		case TypeGame.CopsAndRobbers:
		case TypeGame.SkyBlock:
		case TypeGame.RocketLeague:
		case TypeGame.Pixelmon:
		case TypeGame.WalkingRobots:
			break;
		}
		if (GameController.instance.myPlayer != null)
		{
			spawnPoint.position = GameController.instance.myPlayer.transform.position;
			spawnPoint.rotation = GameController.instance.myPlayer.transform.rotation;
			GameController.instance.removePlayerFromList(GameController.instance.playerScript);
			PhotonNetwork.Destroy(GameController.instance.myPlayer);
			reconnectRoom = true;
			RocketLeagueCotroller.instance.rocketLeagueStarted = false;
			if (CopsAndRobbersController.instance != null)
			{
				CopsAndRobbersController.instance.gameIsStarted = false;
			}
			if (!settings.offlineMode)
			{
				PhotonNetwork.isMessageQueueRunning = true;
			}
		}
		if (settings.offlineMode)
		{
			GameObject original = (GameObject)Resources.Load("Player", typeof(GameObject));
			myPlayer = (GameObject)Object.Instantiate(original, spawnPoint.position, spawnPoint.rotation);
			settings.IncreaseRoundCountForGameMode(TypeGame.Offline, 1);
			StarterPacksManager.instance.RoundCountEvents(TypeGame.Offline, settings.GetRoundCountForGameMode(TypeGame.Offline));
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Tier " + CupsManager.CurrentTier, "Offline_sandbox ");
			FlurryWrapper.LogEvent(FlurryWrapper.EV_MULTIPLAYER_MODE_RATING, dictionary);
			dictionary.Clear();
			dictionary.Add("Round", "Offline_sandbox");
			FlurryWrapper.LogEvent(FlurryWrapper.EV_ROUNDS_COUNT_BY_MODES, dictionary);
		}
		else
		{
			myPlayer = PhotonNetwork.Instantiate("Player", spawnPoint.position, spawnPoint.rotation, 0);
			if (!reconnectRoom)
			{
				settings.IncreaseRoundCountForGameMode(TypeGame.OnlineSandbox, 1);
				StarterPacksManager.instance.RoundCountEvents(TypeGame.OnlineSandbox, settings.GetRoundCountForGameMode(TypeGame.OnlineSandbox));
			}
		}
		if (myPlayer == null)
		{
			return;
		}
		playerScript = myPlayer.GetComponent<PlayerBehavior>();
		if (!settings.offlineMode)
		{
			playerScript.startPVP = false;
			PhotonNetwork.player.UpdateProperty(PlayerPropsKeys.nickname, settings.tekName);
			PhotonNetwork.player.UpdateProperty(PlayerPropsKeys.playerId, BCWServer.idPlayer);
			if (typeGame == TypeGame.TDM || typeGame == TypeGame.Zombe)
			{
				TDMController.instance.WatchPlayer(playerScript);
			}
		}
		GameController.instance.myPlayer = myPlayer;
		GameController.instance.playerScript = playerScript;
		GameController.instance.weaponManagerScripts = myPlayer.GetComponent<WeaponManager>();
		playerScript.SetVisiblePlayerBesidesControl(false);
		if (reconnectRoom)
		{
			playerScript.transform.eulerAngles = Vector3.up * playerScript.transform.eulerAngles.y;
			playerScript.Points = reconPoint;
			afterRecconect = true;
			CoroutineRunner.Invoke(5f, delegate
			{
				afterRecconect = false;
			});
			StartCoroutine(WaitForRound());
			playerScript.mecanimWrapper.StartCheck(playerScript.meshInCam);
			RaycastHit hitInfo;
			if (Physics.Raycast(reconPosition + Vector3.up, Vector3.down, out hitInfo, 150f))
			{
				reconPosition = hitInfo.point;
			}
			if (Mathf.Abs(reconPosition.y) > 500f)
			{
				SpawnPointChief.SpawnPoint point = playerScript.GetPoint(playerScript);
				reconPosition = point.position;
				reconRotation = point.rotation;
			}
			if (GameController.instance.curTypeGame != TypeGame.RocketLeague)
			{
				playerScript.Respawn(reconPosition, reconRotation);
			}
			else if (RocketLeagueCotroller.instance.inRestartWindow)
			{
				GameInfoWindow.instance.ShowEndGameWindow();
			}
			else
			{
				GameInfoWindow.instance.ShowStartGameWindow();
			}
			stopRecconect();
			if (playerInCar != null)
			{
				playerInCar.reset();
				playerInCar = null;
			}
			CameraModeControl.instance.OnRespawn();
			GameController.instance.switchInterface(false);
			GameController.instance.showWindowGame();
			MarketController.Instance.HideMarket();
			CoroutineRunner.Invoke(1.5f, delegate
			{
				if (PVPManager.instance != null && GameInfoWindow.instance != null && PVPManager.instance.roundIndex != reconnectRoundIndex)
				{
					BCWDebug.LogError("************* Реконнектные дела!!! ************  PVPManager.instance.roundIndex = " + PVPManager.instance.roundIndex + "            reconnectRoundIndex = " + reconnectRoundIndex, "zombie");
					playerScript.Points = 0;
					PVPManager.instance.TimeOut();
					PVPManager.instance.needToCheckRoundIndexAfterPause = false;
					BCWDebug.Log("Time out on reconnect", "Pause Round Change");
				}
			});
			if (GameController.instance != null && GameController.instance.curTypeGame == TypeGame.Zombe)
			{
				if (reconMyTeam == PunTeams.Team.red)
				{
					TDMController.instance.SendMasterMyTeam(playerScript.photonView.ownerId, reconMyTeam);
				}
				playerScript.maxHealth = reconMaxHealth;
				playerScript.health = reconHealth;
				playerScript.localTeam = reconMyTeam;
				TDMController.myTeam = reconMyTeam;
				CoroutineRunner.Invoke(1f, delegate
				{
					playerScript.RiseEvent(playerScript, "OnPlayerDeath", -9999, true);
					playerScript.RiseEvent(playerScript, "OnPlayerRespawn");
				});
			}
		}
		else
		{
			StartCoroutine(WaitLoadSectors());
		}
		reconPoint = 0;
		if (!settings.offlineMode && !PhotonNetwork.isMasterClient)
		{
			PVPManager.instance.ReadyToTakeInfo();
		}
		reconnectRoom = false;
		if (typeGame == TypeGame.normal && !settings.offlineMode)
		{
			Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
			string text = ((!string.IsNullOrEmpty(PhotonNetwork.room.customProperties["pass"].ToString())) ? "private" : string.Empty);
			dictionary2.Add("Tier " + CupsManager.CurrentTier, "Online_sandbox " + text);
			FlurryWrapper.LogEvent(FlurryWrapper.EV_MULTIPLAYER_MODE_RATING, dictionary2);
			dictionary2.Clear();
			dictionary2.Add("Round", "Online_sandbox");
			FlurryWrapper.LogEvent(FlurryWrapper.EV_ROUNDS_COUNT_BY_MODES, dictionary2);
		}
		BCWServer.instance.SendInfoPVP();
		if (typeGame == TypeGame.SkyBlock)
		{
			StartCoroutine(WaitForGetTeamInSkyBlock());
		}
		if ((bool)FriendsController.instance)
		{
			if (!settings.offlineMode && GameController.instance.curTypeGame != TypeGame.race && FriendsController.instance.IdsFrinedsInviteToBattle.Count > 0)
			{
				FriendsController.instance.InviteFriendToBattle(FriendsController.instance.IdsFrinedsInviteToBattle, null);
			}
			FriendsController.instance.IdsFrinedsInviteToBattle.Clear();
		}
	}

	private IEnumerator WaitLoadSectors()
	{
		PhotonNetwork.isMessageQueueRunning = true;
		if (GameController.instance.curTypeGame == TypeGame.normal)
		{
			playerScript.FirstSpawn(2f);
		}
		ManagerPreloadingSectors.thisScript.StartCheck();
		yield return null;
		while (ManagerPreloadingSectors.thisScript != null && ManagerPreloadingSectors.thisScript.isFirstLoading)
		{
			yield return null;
		}
		if (TDMController.isTeamGameType && PVPManager.instance.GetTime() < reconTime)
		{
			playerScript.Points = 0;
		}
		if ((TDMController.isTeamGameType || GameController.instance.curTypeGame == TypeGame.race) && GameController.instance.curTypeGame != TypeGame.Zombe)
		{
			GameInfoWindow.instance.startWindowHided = false;
			GameInfoWindow.instance.ShowStartGameWindow();
		}
		if (GameController.instance.curTypeGame != TypeGame.race)
		{
			yield break;
		}
		playerScript.AskRespawnIndex();
		if (!PhotonNetwork.isMasterClient)
		{
			int oldMasterId = PhotonNetwork.masterClient.ID;
			while (!PlayerBehavior.MyPlayer.recieveRespawnIndex)
			{
				if (PhotonNetwork.isMasterClient || oldMasterId != PhotonNetwork.masterClient.ID)
				{
					playerScript.AskRespawnIndex();
					oldMasterId = PhotonNetwork.masterClient.ID;
				}
				yield return null;
			}
		}
		InitLevel();
		RaceController.instance.SpawnCar();
		RaceController.instance.Restart();
		yield return null;
		PlayerBehavior.MyPlayer.playerReadyRace = true;
	}

	private IEnumerator WaitForRound()
	{
		yield return new WaitForEndOfFrame();
		if (_disconnectRound < PVPManager.instance.CurrentRound)
		{
			playerScript.Points = 0;
		}
	}

	private IEnumerator WaitForGetTeamInSkyBlock()
	{
		while (TDMController.myTeam == PunTeams.Team.none)
		{
			yield return new WaitForSeconds(0.5f);
		}
		if (reconMyTeam == PunTeams.Team.none)
		{
			yield break;
		}
		if (TDMController.myTeam == reconMyTeam)
		{
			if (SkyBlockController.instance.hasFlag)
			{
				PlayerBehavior.MyPlayer.SwitchFlag(true);
			}
		}
		else
		{
			SkyBlockController.instance.hasFlag = false;
		}
		SkyBlockController.instance.UpdateColors();
	}

	public void OnLeftRoom()
	{
		if (!reconnectRoom && !Load.LoadBool("ForceReconnect") && !controllerConnectPhoton.thisScript.changeMap)
		{
			PhotonNetwork.player.SetCustomProperties(PVPManager.WipedHash(PhotonNetwork.player.customProperties));
			GameController.instance.exitAfterDisconect();
			reconPoint = 0;
		}
		if (controllerConnectPhoton.thisScript.changeMap)
		{
			reconPoint = 0;
		}
		if (RaceController.instance != null)
		{
			RaceController.instance.ResetFlags();
		}
	}

	public void SaveSettingsPlayer()
	{
		reconPoint = playerScript.Points;
		reconTime = PVPManager.instance.GetTime();
		reconHealth = playerScript.health;
		reconMaxHealth = playerScript.maxHealth;
		reconTeam = PlayerBehavior.MyPlayer.localTeam;
		reconMyTeam = TDMController.myTeam;
		reconMaxArmor = playerScript.maxArmor;
		reconPosition = myPlayer.transform.position;
	}

	public void stopRecconect()
	{
		reconnectRoom = false;
		ReconnectCanceled = true;
		reconTeam = PunTeams.Team.none;
		reconPoint = 0;
		if (reconnect != null)
		{
			StopCoroutine(reconnect);
		}
		ActivityIndicator.activEnabled = false;
	}

	public void ConnectAfterForceDisconnect()
	{
		BCWDebug.Log("ConnectAfterForceDisconnect");
		if (Load.LoadBool("ForceReconnect") && !settings.offlineMode)
		{
			OnConnectionFail(DisconnectCause.DisconnectByServerTimeout);
		}
	}

	public void ActivateRoomAfterDelay()
	{
		PhotonNetwork.room.visible = true;
		BCWDebug.Log("ActivateRoom = " + PhotonNetwork.room.visible);
	}

	private void OnConnectionFail(DisconnectCause cause)
	{
		if (settings.offlineMode)
		{
			return;
		}
		if (GameController.instance != null && PanelEndGameRewardUI.instance != null && GameController.instance.curTypeGame == TypeGame.normal)
		{
			PanelEndGameRewardUI.instance.GiveRewards();
			return;
		}
		BCWDebug.Log("reconnect");
		countConnectToRoom = 0;
		reconnectRoom = true;
		ReconnectCanceled = false;
		RocketLeagueCotroller.instance.rocketLeagueStarted = false;
		if (CopsAndRobbersController.instance != null)
		{
			CopsAndRobbersController.instance.gameIsStarted = false;
		}
		if (TDMController.instance != null)
		{
			TDMController.instance.ClearTeams();
		}
		reconnect = null;
		_disconnectRound = PVPManager.instance.CurrentRound;
		PVPManager.instance.ResetRoundCount();
		if (PVPManager.instance != null)
		{
			reconnectRoundIndex = PVPManager.instance.roundIndex;
			PVPManager.instance.startRoundTimeOnPause = PVPManager.instance.TimeOfRoundStart();
			PVPManager.instance.needToCheckRoundIndexAfterPause = true;
		}
		if (GameController.instance.curTypeGame == TypeGame.CopsAndRobbers)
		{
			Invoke("OnLeftRoom", 15f);
			reconnectRoom = false;
		}
		else
		{
			Invoke("ConnectToPhoton", 3f);
		}
		if (GameController.instance != null)
		{
			GameController.instance.hideAllPanels();
		}
		if (GameController.instance.playerScript != null)
		{
			SaveSettingsPlayer();
			if (GameController.instance.playerScript.inCar || GameController.instance.playerScript.inHelic)
			{
				playerInCar = GameController.instance.playerScript.currentCar;
				if (GameController.instance.curTypeGame != TypeGame.race && GameController.instance.curTypeGame != TypeGame.tanks && GameController.instance.curTypeGame != TypeGame.WalkingRobots)
				{
					GameController.instance.playerScript.GetOutOfCar(true);
				}
			}
			GameController.instance.switchInterface(false);
			GameController.instance.playerScript.tController.jetpack.DeactivateFromDisconnect();
		}
		GameController.instance.playerScript.RemoveAllGrenade();
		TDMController.myTeam = PunTeams.Team.none;
		GameController.instance.listPlayers.Clear();
		if ((bool)ControlsPositionsSaver.instance && ControlsPositionsSaver.instance.gameObject.activeSelf)
		{
			ControlsPositionsSaver.instance.HideThis();
		}
		MarketController.Instance.HideMarket();
		if (GameController.instance.curTypeGame == TypeGame.race)
		{
			controllerConnectPhoton.thisScript.changeMap = true;
			if (RaceController.instance != null)
			{
				RaceController.instance.ResetFlags();
			}
		}
		MessageWindow.instance.showPanelReconnect();
		if (!settings.offlineMode)
		{
			PhotonNetwork.isMessageQueueRunning = true;
		}
	}

	private void ConnectToPhoton()
	{
		if (!ReconnectCanceled)
		{
			BCWDebug.Log("ConnectToPhoton");
			PhotonNetwork.autoJoinLobby = true;
			PhotonNetwork.isMessageQueueRunning = true;
			if (reconnect == null)
			{
				reconnect = Reconnect();
				StartCoroutine(reconnect);
			}
		}
	}

	private IEnumerator Reconnect()
	{
		float time = Time.time;
		while (!PhotonNetwork.connectedAndReady)
		{
			Debug.Log(string.Concat(PhotonNetwork.connectionState, " ", PhotonNetwork.connectionStateDetailed));
			if (PhotonNetwork.connectionState == ConnectionState.Connected)
			{
				reconnect = null;
				countConnectToRoom = 0;
				yield break;
			}
			PhotonNetwork.ConnectUsingSettings(settings.verConnectPhoton);
			yield return new WaitForSeconds(5f);
			if (Time.time - time > 25f)
			{
				reconnectRoom = false;
				OnLeftRoom();
				reconnect = null;
				yield break;
			}
		}
		reconnect = null;
	}

	private void OnFailedToConnectToPhoton(object parameters)
	{
		BCWDebug.Log("OnFailedToConnectToPhoton. StatusCode: " + parameters);
		if (!ReconnectCanceled && !settings.offlineMode)
		{
			Invoke("ConnectToPhoton", 3f);
		}
	}

	public void OnJoinedLobby()
	{
		BCWDebug.Log("OnJoinedLobby " + PhotonNetwork.lobby.Name);
		if (joinRoom != null)
		{
			StopCoroutine(joinRoom);
		}
		joinRoom = WaitAndConnectToRoom();
		StartCoroutine(joinRoom);
	}

	public IEnumerator WaitAndConnectToRoom()
	{
		while (PhotonNetwork.connectionStateDetailed != ClientState.ConnectedToMaster && PhotonNetwork.connectionStateDetailed != ClientState.JoinedLobby)
		{
			yield return new WaitForSeconds(0.5f);
		}
		ConnectToRoom();
	}

	public void ConnectToRoom()
	{
		CancelInvoke("OnLeftRoom");
		if (!controllerConnectPhoton.thisScript.changeMap)
		{
			if (!settings.offlineMode && !ReconnectCanceled)
			{
				BCWDebug.Log(" ConnectToRoom " + Load.LoadString(settings.keyRoomName));
				if (!PhotonNetwork.JoinRoom(Load.LoadString(settings.keyRoomName)))
				{
					GameController.instance.exitToMenu();
				}
			}
		}
		else
		{
			reconnectRoom = false;
		}
	}

	private void OnApplicationPause(bool pause)
	{
		BCWDebug.LogError("OnApplicationPause(bool  pause) " + pause, "controllerConnect");
		if (pause)
		{
			PhotonHandler.StopFallbackSendAckThread();
			return;
		}
		PhotonHandler.StartFallbackSendAckThread();
		if (!settings.offlineMode)
		{
			PhotonNetwork.isMessageQueueRunning = true;
		}
	}

	private void OnPhotonJoinRoomFailed()
	{
		BCWDebug.Log("OnPhotonJoinRoomFailed - init");
		if (!settings.offlineMode && !controllerConnectPhoton.thisScript.changeMap)
		{
			countConnectToRoom++;
			if (countConnectToRoom < 6)
			{
				Invoke("ConnectToRoom", 3f);
				return;
			}
			BCWDebug.Log("reconnect failed");
			GameController.instance.exitAfterDisconect();
		}
	}

	private void OnJoinedRoom()
	{
		Debug.Log("OnJoinedRoom");
		if (RaceController.instance != null)
		{
			RaceController.instance.leaveRoom = false;
			if (GameInfoWindow.instance != null)
			{
				GameInfoWindow.instance.startWindowHided = false;
			}
		}
		if (MessageWindow.instance != null)
		{
			MessageWindow.instance.hidePanelReconnect();
		}
	}

	public void OnPhotonPlayerConnected(PhotonPlayer player)
	{
		if (GameController.instance.curTypeGame == TypeGame.SkyBlock)
		{
			StartCoroutine("WaitForNewPlayerTeam", player);
		}
	}

	private IEnumerator WaitForNewPlayerTeam(PhotonPlayer photonPlayer)
	{
		PlayerBehavior player = GameController.instance.FindPlayer(photonPlayer.ID);
		while (player == null || player.localTeam == PunTeams.Team.none)
		{
			yield return new WaitForSeconds(0.5f);
			if (player == null)
			{
				player = GameController.instance.FindPlayer(photonPlayer.ID);
			}
		}
		if (player.localTeam != 0 && SkyBlockController.instance.hasFlag)
		{
			PlayerBehavior.MyPlayer.photonView.RPC("SwitchFlag", PhotonPlayer.Find(player.ID), true);
		}
	}

	public void SendPVPinfo()
	{
		if (_sendPVPinfo != null)
		{
			StopCoroutine(_sendPVPinfo);
		}
		_sendPVPinfo = Sendinfo();
		StartCoroutine(_sendPVPinfo);
	}

	private IEnumerator Sendinfo()
	{
		yield return new WaitForEndOfFrame();
		if (playerScript != null)
		{
			foreach (PlayerBehavior curPlayer in GameController.instance.listPlayers)
			{
				if (!(curPlayer == null))
				{
					if (curPlayer.inCar || curPlayer.inHelic)
					{
						curPlayer.GetInCarWithConfirmation(curPlayer.viewId, curPlayer.currentCar.viewId);
					}
					else
					{
						curPlayer.showOurGun();
					}
				}
			}
		}
		yield return new WaitForSeconds(1f);
	}

	public void OnPhotonPlayerDisconnected(PhotonPlayer player)
	{
		BCWDebug.Log("OnPlayerDisconnecedinit: " + player.ID);
		GameController.instance.removePlayerWithID(player.ID);
	}

	public void OnReceivedRoomList()
	{
		BCWDebug.Log("OnReceivedRoomListinit");
	}

	public void OnReceivedRoomListUpdate()
	{
		BCWDebug.Log("OnReceivedRoomListUpdateinit");
	}

	public void OnConnectedToPhoton()
	{
		BCWDebug.Log("OnConnectedToPhotoninit");
	}

	public void OnPhotonInstantiate(PhotonMessageInfo info)
	{
		BCWDebug.Log("OnPhotonInstantiate init" + info.sender);
	}
}
